var util = {
      files: {
		js: [],
		css: []
	},
      version: '1.0.0',
	init: function() {
		var that = this;
            that.tab();
	},
      load: function(_src, callback, _href) {
		var that = this;
		that.js(_src, callback);
		_href && that.css(_href);
	},
	js: function(_src, callback) {
		var that = this,
			_v = '',
			_script = document.createElement("script");
		if (_src.indexOf('?v=') != -1) {
			var arr = _src.split('?v=');
			_src = arr[0];
			_v = arr[1];
		}
		if (that.files.js.indexOf(_src) == -1) {
			_script.type = "text/javascript";
			_script.src = _src + '?v=' + that.version + _v;
			document.getElementsByTagName('head')[0].appendChild(_script);
			if (! /*@cc_on!@*/ 0) {
				_script.onload = function() {
					that.loaded();
					callback();
				}
			} else {
				_script.onreadystatechange = function() {
					if (_script.readyState == 'loaded' || _script.readyState == 'complete') {
						that.loaded();
						callback();
					}
				}
			}
			that.files.js.push(_src);
		}
	},
      loading: function() {
		!$('#loading', top.document)[0] && $(top.document.body).append('<div id="loading"><img src="' + __LIB_ROOT__ +
			'/images/loading.gif"/></div>');
	},
	loaded: function() {
		$('#loading', top.document)[0] && $('#loading', top.document).remove();
	},
      tab: function($o) {
		var $hover = $o ? $o.find('[data-tab-hover-target]') : $('[data-tab-hover-target]'),
			$click = $o ? $o.find('[data-tab-target]') : $('[data-tab-target]'),
			doFun = function($this, data_target) {
				if ($this.hasClass('active')) {
					return false;
				}
				var _target = $this.attr(data_target),
					$target = $('[data-tab-item="' + _target + '"]'),
					$nav = $this.parents('.tab-nav'),
					$items = $target.parents('.tab-items');
				$nav.find('[' + data_target + ']').removeClass('active');
				$this.addClass('active');
				$items.children('.tab-item').removeClass('active');
				$target.addClass('active');
			};
		$click.on('click', function() {
			doFun($(this), 'data-tab-target');
		});
		$hover.mouseenter(function() {
			doFun($(this), 'data-tab-hover-target');
		});
	},
      swiper: function(options) {
		var that = this,
			doFun = function() {
				for (var i = 0; i < that.swiper_options.length; i++) {
					new util_swiper(that.swiper_options[i]);
				}
                for (var i = 0; i < that.swiper_options.length; i++) {
					that.swiper_options.splice(i, 1);
				}
			};
		that.swiper_options = that.swiper_options || [];
		that.swiper_options.push(options);
		if (typeof(util_swiper) == 'undefined') {
			util.load('./static/js/swiper.js', function() {
				doFun();
			});
		} else {
			doFun();
		}
	},
	isShow: function($el) {
		if( $el.length && $el.is(':visible') ){
			var scroll_top = $(window).scrollTop(),
				window_height = $(window).height(),
				offset_top = $el.offset().top,
				height = $el.height();
			return scroll_top <= offset_top + height && scroll_top >= offset_top - window_height;
		}else{
			return false;
		}
	},
}
$(function() {
	util.init();
});
